﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using EDC.Logic.CSV;
using System.IO;
using EDC.Common.Exceptions;

namespace EDC.Logic.Tests
{
    [TestFixture]
    public class CSVFileTests
    {
        [TestCase]
        [Category("CSVFile")]
        public void DefaultConstructorTest()
        {
            CSVFile csv = new CSVFile();

            Assert.IsNullOrEmpty(csv.FileName);
            Assert.AreEqual((char)0, csv.Separator);
            Assert.IsFalse(csv.IsOpen);
        }

        [TestCase]
        [Category("CSVFile")]
        public void ParametrizedConstructor2ArgsTest()
        {
            CSVFile csv = new CSVFile("File", ';');

            Assert.IsNotNullOrEmpty(csv.FileName);
            Assert.AreEqual("File", csv.FileName);
            Assert.AreEqual(';', csv.Separator);
            Assert.IsFalse(csv.IsOpen);
        }

        [TestCase]
        [Category("CSVFile")]
        public void ParametrizedConstructor3ArgsTest()
        {
            CSVFile csv = new CSVFile("File", false, ';');

            Assert.IsNotNullOrEmpty(csv.FileName);
            Assert.AreEqual("File", csv.FileName);
            Assert.AreEqual(';', csv.Separator);
            Assert.IsFalse(csv.IsOpen);
        }

        [TestCase]
        [Category("CSVFile")]
        [ExpectedException(typeof(ArgumentException))]
        public void OpenFileEmptyFileNameExceptionTest()
        {
            CSVFile csv = new CSVFile();

            csv.Open();
        }

        [TestCase]
        [Category("CSVFile")]
        [ExpectedException(typeof(DirectoryNotFoundException))]
        public void OpenFileWrongFileExceptionTest()
        {
            CSVFile csv = new CSVFile(@"c:/some/file.csv");

            csv.Open();
        }

        [TestCase]
        [Category("CSVFile")]        
        public void OpenFileTest()
        {
            CSVFile csv = new CSVFile(@"..\..\..\TestData.txt");

            csv.Open();

            Assert.IsTrue(csv.IsOpen);
        }

        [TestCase]
        [Category("CSVFile")]
        public void ReadDataTest()
        {
            CSVFile csv = new CSVFile(@"..\..\..\TestData.txt");

            csv.Open();

            Assert.AreEqual("1,2", csv[1]);
            Assert.AreEqual("2", csv[1, 1]);            
        }

        [TestCase]
        [Category("CSVFile")]
        [ExpectedException(typeof(IndexOutOfRangeException))]
        public void ReadRowDataOutOfRangeFirstIndexTest()
        {
            CSVFile csv = new CSVFile(@"..\..\..\TestData.txt");

            csv.Open();

            Assert.AreEqual("1,2", csv[5]);            
        }

        [TestCase]
        [Category("CSVFile")]
        [ExpectedException(typeof(IndexOutOfRangeException))]
        public void ReadColumnDataOutOfRangeFirstIndexTest()
        {
            CSVFile csv = new CSVFile(@"..\..\..\TestData.txt");

            csv.Open();

            Assert.AreEqual("1,2", csv[5,1]);
        }

        [TestCase]
        [Category("CSVFile")]
        [ExpectedException(typeof(IndexOutOfRangeException))]
        public void ReadColumnDataOutOfRangeSecondIndexTest()
        {
            CSVFile csv = new CSVFile(@"..\..\..\TestData.txt");

            csv.Open();

            Assert.AreEqual("1,2", csv[1, 10]);
        }

        [TestCase]
        [Category("CSVFile")]
        [ExpectedException(typeof(NotLoadedDataException))]
        public void ReadRowDataNotLoadedDataExceptionTest()
        {
            CSVFile csv = new CSVFile();            

            Assert.AreEqual("1,2", csv[5]);
        }

        [TestCase]
        [Category("CSVFile")]
        [ExpectedException(typeof(NotLoadedDataException))]
        public void ReadColumnDataNotLoadedDataExceptionTest()
        {
            CSVFile csv = new CSVFile();            

            Assert.AreEqual("1,2", csv[5, 1]);
        }
    }
}
